package com.huangyi.rabbitmq_amqp.routing;

import com.huangyi.rabbitmq_amqp.config.Constants;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer1 {
    public static void main(String[] args) throws IOException, TimeoutException {

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(Constants.HOST);
        factory.setPort(Constants.PORT);
        factory.setVirtualHost(Constants.VIRTUAL_HOST);
        factory.setUsername(Constants.USER);
        factory.setPassword(Constants.PASSWORD);

        Connection connection = factory.newConnection();
        Channel channel  = connection.createChannel();

        channel.queueDeclare(Constants.ROUTING_QUEUE1,true,false,false,null);
        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("consumer1 消费完毕" + new String(body));
            }
        };
        channel.basicConsume(Constants.ROUTING_QUEUE1,true,consumer);
        channel.close();
        connection.close();
    }
}
